package com.suvel.study.service.impl;

import com.suvel.study.service.EmailService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.util.StringUtils;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Override
    public Boolean sendEmailCode(String email, String code) {
        boolean result = true;
        try {
            if (StringUtils.isEmpty(email) || StringUtils.isEmpty(code)) {
                throw new RuntimeException("参数错误，验证邮件发送错误！");
            }
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject("注册验证码");
            message.setText("验证码：" + code);
            message.setTo(email);
            message.setCc("1356919124@qq.com");
            message.setFrom("1356919124@qq.com");
            javaMailSender.send(message);
        } catch (Exception ex) {
            ex.printStackTrace();
            result = false;
        }
        return result;
    }
}
